<template>
	<view>
		<!-- 搜索框 -->
		<view class="search-box">
			<uni-search-bar 
				v-model="value" 
				@confirm="search" 
				@input="handleInput" 
				:radius="100" 
				:focus="true" 
				cancelButton="auto" 
				placeholder="搜索">
			</uni-search-bar>
		</view>

		<!-- 热门搜索 -->
		<view class="hot-box">
			<view class="hot-title">
				<text>热门搜索<uni-icons type="fire-filled" size="16"></uni-icons></text>
			</view>
			<view class="hot-list">
				<uni-tag :inverted="true" type="primary" text="一只狗" @click="clickSearch('一只狗')"></uni-tag>
			</view>
		</view>

		<!-- 搜索建议列表 -->
		<view class="sugg-list" v-if="searchResults.length !== 0">
			<view class="sugg-item" v-for="(item, i) in searchResults" :key="i" @click="gotoDetail(item)">
				<view class="goods-name">{{ item.name }}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>

		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="16" @click="clean"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag :inverted="true" type="primary" :text="item" v-for="(item, i) in histores" :key="i" @click="clickSearch(item)">
				</uni-tag>
			</view>
		</view>

		<!-- 搜索结果 -->
		<view class="search-result">
			<view class="result-card" v-for="(item, index) in result" :key="index" @click="goToDetail(item.goodsID)">
				<image :src="item.goodsImgUrl" class="result-image" mode="aspectFill"></image>
				<view class="result-info">
					<text class="result-name">{{ item.goodsName }}</text>
					<text class="result-price">￥{{ item.price }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import service from '../../utils/request.js';
	export default {
		data() {
			return {
				timer: null,
				kw: '',
				searchResults: [],
				historyList: [],
				value: '',
				result: []
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]');
			this.getSearchData();
		},
		methods: {
			// 防抖处理输入事件
			handleInput(e) {
				clearTimeout(this.timer);
				this.timer = setTimeout(() => {
					this.kw = e;
					this.getSearchData();
				}, 500);
			},

			// 搜索确认方法
			search() {
				this.kw = this.value;
				this.getSearchData();
			},

			// 点击历史记录或热门搜索直接搜索
			clickSearch(name) {
				this.value = name; // 将点击的关键词复制到搜索框
				this.search(); // 立即执行搜索
			},

			// 跳转到详情页
			goToDetail(goodsID) {
				uni.setStorageSync('goodsID', goodsID);
				uni.navigateTo({
					url: `/subpages/detail/detail?goodsID=${goodsID}`
				});
			},

			// 保存搜索关键词到历史记录
			saveSearchHistory() {
				const set = new Set(this.historyList);
				set.delete(this.kw);
				set.add(this.kw);
				this.historyList = Array.from(set);
				uni.setStorageSync('kw', JSON.stringify(this.historyList));
			},

			// 清空搜索记录
			clean() {
				this.historyList = [];
				uni.setStorageSync('kw', '[]');
			},

			// 获取搜索结果
			async getSearchData() {
				if (this.kw.length === 0) {
					this.result = [];
					return;
				}
				try {
					const response = await service.get(`/pets/lists/${this.value}`);
					this.result = response.data;
					this.saveSearchHistory();
				} catch (error) {
					console.error('获取数据失败:', error);
				}
			}
		},
		computed: {
			histores() {
				return [...this.historyList].reverse();
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		background-color: #55aaff;
		position: sticky;
		z-index: 999;
		padding: 10px;
	}

	.sugg-list, .history-box, .search-result {
		padding: 0 10px;
	}

	.sugg-item, .history-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 10px 0;
		border-bottom: 1px solid #efefef;
	}

	.goods-name {
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		margin-right: 10px;
	}

	.history-list, .hot-list {
		display: flex;
		flex-wrap: wrap;

		.uni-tag {
			margin: 5px;
		}
	}

	.search-result {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
	}

	.result-card {
		width: 48%;
		background-color: #fff;
		border-radius: 10px;
		margin-bottom: 10px;
		overflow: hidden;
		box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
		cursor: pointer;
	}

	.result-image {
		width: 100%;
		height: 150px;
	}

	.result-info {
		padding: 10px;
	}

	.result-name {
		font-size: 14px;
		color: #333;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.result-price {
		font-size: 16px;
		color: #ff4d4f;
		margin-top: 5px;
	}
</style>